package org.lc.stk.web.controller.question;

import jakarta.validation.Valid;

import org.lc.stk.service.question.FillBlankQuestionService;
import org.lc.stk.web.dto.question.FillBlankQuestionDTO;
import org.lc.stk.web.response.ApiResponse;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;

@RestController
@RequestMapping("/api/v1/questions/fill-blank")
@Tag(name = "填空题管理", description = "填空题的增删改查接口")
public class FillBlankQuestionController {

    private final FillBlankQuestionService fillBlankQuestionService;

    public FillBlankQuestionController(FillBlankQuestionService fillBlankQuestionService) {
        this.fillBlankQuestionService = fillBlankQuestionService;
    }

    @PostMapping
    @Operation(summary = "创建填空题")
    @PreAuthorize("hasAnyRole('TEACHER', 'ADMIN')")
    public ResponseEntity<ApiResponse<FillBlankQuestionDTO>> createQuestion(
            @Parameter(description = "填空题信息") @Valid @RequestBody FillBlankQuestionDTO questionDTO) {
        FillBlankQuestionDTO created = fillBlankQuestionService.createQuestion(questionDTO);
        return ResponseEntity.ok(ApiResponse.success(created));
    }

    @PutMapping("/{id}")
    @Operation(summary = "更新填空题")
    @PreAuthorize("hasAnyRole('TEACHER', 'ADMIN')")
    public ResponseEntity<ApiResponse<FillBlankQuestionDTO>> updateQuestion(
            @Parameter(description = "填空题ID") @PathVariable Long id,
            @Parameter(description = "填空题信息") @Valid @RequestBody FillBlankQuestionDTO questionDTO) {
        FillBlankQuestionDTO updated = fillBlankQuestionService.updateQuestion(id, questionDTO);
        return ResponseEntity.ok(ApiResponse.success(updated));
    }
}